package com.dzm.config;

import com.dzm.constants.ClientConstant;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * 作者：Dzm
 * 时间：2024-08-15-16:45
 * 功能：
 *
 */

@Configuration
public class MyRequestInterceptor {

    @Bean
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                //先得到原来的请求
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = attributes.getRequest();
                //2.得到原来的请求头
                String Authorization = request.getHeader(ClientConstant.CLIENT_TOKEN_NAME);
                //3.将其设置到新请求的请求头
                requestTemplate.header(ClientConstant.CLIENT_TOKEN_NAME, Authorization);

            }
        };
    }
}
